import { App, Inject, OnWSConnection, OnWSMessage, Provide, WSController, WSEmit } from "@midwayjs/core";
import { Application, Context } from "@midwayjs/socketio";

@Provide()
@WSController("/testPath")
export class HelloController {

    @Inject()
    ctx: Context

    @App("socketIO")
    socketApp: Application;

    @OnWSConnection()
    async onConnectionMethod() {
        console.log("on client connect", this.ctx.id);
        // this.ctx.join()
        console.log(this.ctx.rooms);
        this.ctx.join("room1")
        console.log(this.ctx.rooms);
        console.log(this.ctx.app.of("testPath").adapter.rooms.entries());
        for (const [key, iterator] of this.ctx.app.of("testPath").adapter.rooms) {
            console.log(iterator.size);
            console.log(key);

            // if (iterator.length) {

            // }
        }
    }

    @OnWSMessage("myMvent")
    @WSEmit("myEventResult")
    async gotMessage(...arg: any[]) {
        console.log("on data got", this.ctx.id, arg);
        console.log(this.socketApp.engine.clientsCount);
        console.log(this.socketApp.of("testPath").sockets.size);

        return { data: arg, id: this.ctx.id }
    }

    @WSEmit("myEventResult")
    async sendMessage() {
        // this.socketApp.j
        this.socketApp.of("testPath").emit("myEventResult", { data: 1 })
        // return {data:111}
    }
}